USE [sesamo]
GO

/****** Object:  StoredProcedure [dbo].[get_cotizacion_detalle]    Script Date: 13/04/2014 05:09:44 p.m. ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[get_cotizacion_detalle]
    @inicio varchar(10),
	@fin varchar(10),
	@rows varchar(10),
	@idCotizacion varchar(20),
	@ultimoId INT = NULL OUTPUT
AS
BEGIN
    DECLARE @query nvarchar(4000);
   set @query='SELECT TOP('+@rows+') * FROM(
				SELECT rank() over(order by idDetalle) as row, 
						   idcotizacion,
				           codigo,
						   cantidad,
						   precio
						   from dbo.encuestas_cotizacion_detalle where idcotizacion = '+@idCotizacion+'
						  
				)as encuestas  WHERE row BETWEEN '+@inicio+' AND '+@fin;

				EXEC (@query)
	SET @ultimoId = SCOPE_IDENTITY();
END

GO

